import { fetchSelectOpts } from '@/service/api';

const DataSelectModel = {
  namespace: 'dataSelect',
  state: {
    selectOptions: {},
  },
  effects: {
    *initItemSelectOptions({ payload }, { call, put }) {
      try {
        const { id } = payload;
        const { list } = yield call(fetchSelectOpts, payload);
        yield put({ type: 'updateSelectDict', payload: { [id]: list } });
        return Promise.resolve();
      } catch (e) {
        console.log(e);
        return Promise.reject();
      }
    },
  },
  reducers: {
    updateSelectDict(state, action) {
      return {
        ...state,
        selectOptions: { ...state.selectOptions, ...action.payload },
      };
    },
  },
};
export default DataSelectModel;
